package com.situ.utils;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;

@Data
@Schema(description = "统一返回结果")
public class Result<T> implements Serializable {

    @Schema(description = "是否成功")
    private boolean flag;
    @Schema(description = "返回码")
    private Integer code;
    @Schema(description = "返回信息")
    private String msg;
    @Schema(description = "响应数据")
    private T data;

    public Result(){}

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public Result(boolean flag, Integer code, String msg) {
        this.flag = flag;
        this.code = code;
        this.msg = msg;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(boolean flag, Integer code, String msg, T data) {
        this.flag = flag;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static Result ok(String msg){
        return new Result(true,200,msg);
    }

    public static Result failure(String msg){
        return new Result(false,201,msg);
    }

    public static <T> Result<T> success(Integer code,String msg,T data){
        return new Result<T>(true,code,msg,data);
    }

    public static <T> Result<T> failure(Integer code,String msg,T data){
        return new Result<T>(false,code,msg,data);
    }

    public static Result failureError(Integer code,String msg){
         return new Result(code,msg);
    }

}